package com.banch.cloud.seata.order.service.impl;

import com.banch.cloud.seata.order.dao.OrderDao;
import com.banch.cloud.seata.order.domain.Order;
import com.banch.cloud.seata.order.feign.AccountService;
import com.banch.cloud.seata.order.feign.StorageService;
import com.banch.cloud.seata.order.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * order service
 *
 * @author banch
 * @version 1.0
 * @date 2020/7/30
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    /**
     * orderDao
     */
    @Resource
    OrderDao orderDao;

    /**
     * accountService
     */
    @Resource
    AccountService accountService;

    /**
     * storageService
     */
    @Resource
    StorageService storageService;


    @GlobalTransactional(name = "myTransaction", rollbackFor = Exception.class)
    @Override
    public void create(Order order) {
        log.info("----->开始新建订单");
        //新建订单
        orderDao.create(order);

        //扣减库存
        log.info("----->订单微服务开始调用库存，做扣减Count");
        storageService.decrease(order.getProductId(), order.getCount());
        log.info("----->订单微服务开始调用库存，做扣减end");

        //扣减账户
        log.info("----->订单微服务开始调用账户，做扣减Money");
        accountService.decrease(order.getUserId(), order.getMoney());
        log.info("----->订单微服务开始调用账户，做扣减end");

        //修改订单状态，从零到1代表已经完成
        log.info("----->修改订单状态开始");
        orderDao.update(order.getId(), 0);
        log.info("----->修改订单状态结束");

        log.info("----->下订单结束了");
    }

}